﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using NSsh.Server.TransportLayer;
using NSsh.Server.TransportLayer.State;
using NSsh.Common.Tests;
using NSsh.Common;

namespace NSsh.Server.Tests.TransportLayer.State
{
    [TestFixture]
    public class ConnectedStateFixture
    {
        private MockRepository _mockRepository;
        private ITransportLayerManager _manager;

        [SetUp]
        public void Setup()
        {
            _mockRepository = new MockRepository();
            _manager = _mockRepository.CreateMock<ITransportLayerManager>();
        }

        [Test]
        public void ShouldProcessCorrectly()
        {
            string clientVersion = "SSH-2.0-test";

            using (_mockRepository.Record())
            {
                _manager.Write(Encoding.ASCII.GetBytes(ConnectedState.VersionString + "\r\n"));

                Expect.Call(_manager.ReadLine()).Return(clientVersion);

                _manager.ClientVersion = clientVersion;

                _manager.ChangeState(TransportLayerState.VersionsExchanged);
            }

            using (_mockRepository.Playback())
            {
                AbstractTransportState abstractState = new ConnectedState();
                abstractState.Process(_manager);
            }
        }

        [Test]
        public void ShouldFailForInvalidClient()
        {
            string clientVersion = "SSH-invalid";

            using (_mockRepository.Record())
            {
                _manager.Write(Encoding.ASCII.GetBytes(ConnectedState.VersionString + "\r\n"));

                Expect.Call(_manager.ReadLine()).Return(clientVersion);
            }

            using (_mockRepository.Playback())
            {
                AbstractTransportState abstractState = new ConnectedState();

                Assert.That(new MethodInvoker(delegate
                {
                    abstractState.Process(_manager);

                }), Throws.Exception<ArgumentException>());
            }
        }
    }
}
